import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

import 'constant.dart';

class AmapLocation {
  factory AmapLocation() {
    if (_instance == null) {
      final MethodChannel methodChannel = const MethodChannel('$AMAP_CHANNEL_NAME/location');
      final EventChannel eventChannel = const EventChannel('$AMAP_CHANNEL_NAME/locationChanged');
      final EventChannel poiEventChannel = const EventChannel('$AMAP_CHANNEL_NAME/getPoiList');
      final EventChannel distanceEventChannel = const EventChannel('$AMAP_CHANNEL_NAME/getDistance');
      _instance = AmapLocation.private(methodChannel, eventChannel, poiEventChannel, distanceEventChannel);
    }

    return _instance;
  }

  AmapLocation.private(this.methodChannel, this.eventChannel, this.poiEventChannel, this.distanceEventChannel);

  static AmapLocation _instance;

  final MethodChannel methodChannel;
  final EventChannel eventChannel;
  final EventChannel poiEventChannel;
  final EventChannel distanceEventChannel;
  Stream<String> onLocationFetched;
  Stream<String> onpoiFetched;
  Stream<String> distanceFetched;

  ///iOS设置高德key
  Future<String> setIOSKey(String key) async {
    return await methodChannel.invokeMethod('setKey', {'key': key});
  }

  //隐私协议
  Future<void> privacy() async {
    return await methodChannel.invokeMethod("privacy");
  }

  ///初始化定位
  Future<void> initGdMap() async {
    return await methodChannel.invokeMethod("initGdMap");
  }

  ///判断GPS定位服务是否开启
  Future<bool> get inspectionGPS => methodChannel.invokeMethod("inspectionGPS");

  ///开始定位
  Future<void> get startLocation => methodChannel.invokeMethod("startLocation");

  ///结束定位
  Future<void> get stopLocation => methodChannel.invokeMethod("stopLocation");

  ///启动前台服务
  Future<void> get startService => methodChannel.invokeMethod("startService");

  ///关闭前台服务
  Future<void> get stopService => methodChannel.invokeMethod("stopService");

  ///获取最新定位信息返回json字符串，获取失败则返回空字符串
  Future<String> get getLocation => methodChannel.invokeMethod("getLocation").then<String>((dynamic result) => result);

  ///持续的获取定位信息返回json字符串，获取失败则返回空字符串
  Stream<String> get onLocationChanged {
    if (onLocationFetched == null) {
      onLocationFetched = eventChannel.receiveBroadcastStream().map((dynamic event) => event);
    }
    return onLocationFetched;
  }

  ///地址搜索
  ///参数:Map<String, dynamic> map = {"address": String, "page": int,"cityCode":String};
  Future<void> locationSearch(Map<String, dynamic> map) async {
    debugPrint('方法:locationSearch dart端参数:-> $map');
    return await methodChannel.invokeMethod("locationSearch", map);
  }

  ///获取定位搜索json字符串，获取失败则返回空字符串
  Stream<String> get getPoiList {
    if (onpoiFetched == null) {
      onpoiFetched = poiEventChannel.receiveBroadcastStream().map((dynamic event) => event);
    }
    return onpoiFetched;
  }

  ///两点间距离计算
  ///LatLng nowLatLng = new LatLng(latitude: num, longitude: num);
  ///参数:Map<String, dynamic> map = {"startLocation": json.encode(nowLatLng), "endLocation": json.encode(mLatLng)};
  Future<String> distanceCompute(Map<String, dynamic> map) async {
    debugPrint('方法:distanceCompute dart端参数:-> $map');
    return await methodChannel.invokeMethod("distanceCompute", map);
  }

  ///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  ///距离搜索
  ///因相应功能废弃,这里弃用~
  Future<String> distanceSearch(Map<String, dynamic> map) async {
    debugPrint('方法:distanceSearch dart端参数:-> $map');
    return await methodChannel.invokeMethod("distanceSearch", map);
  }

  ///获取距离搜索的距离结果，获取失败则返回0
  ///因相应功能废弃,这里弃用~
  Stream<String> get getDistance {
    if (distanceFetched == null) {
      distanceFetched = distanceEventChannel.receiveBroadcastStream().map((dynamic event) => event);
    }
    return distanceFetched;
  }
}
